package com.cpone.cloudweb.utils.bak;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;

public class SerSocket extends Thread{
private Socket socket;
  	private InputStream in;
	private OutputStream out;
	private StringBuffer keyWord_task;
	private StringBuffer voi_task;
	private StringBuffer strCon_down;
	private StringBuffer strCon_notify;
	public SerSocket(Socket s) throws IOException {
		socket = s;
		in = socket.getInputStream();
		out = socket.getOutputStream();
		start();
	}
	
	public void run() {
		File myFile = null;
		RandomAccessFile outNew =null;
		try {
			while (true) {
				byte[] strByte = new byte[4];
		    	in.read(strByte);
		    	byte[] idByte = new byte[4];
		    	in.read(idByte);
		    	byte[] statusByte = new byte[4];
		    	in.read(statusByte);
				
		    	
		    	String str = new String(strByte);
		    	System.out.println("接受到任务通知--包头："+str);
		    	int id = readBigEndianInt(idByte);
		    	System.out.println("接收到任务通知--标识："+id);
		    	int status = readBigEndianInt(statusByte);
		    	System.out.println("接收到任务通知---状态"+status);
		    	
		    	if(str.equals("RWXF")&&id==1&&status==1){
		    		System.out.println("任务接受成功，回复客户端");
		    		byte[] buf = new byte[12];
			    	byte[] bufByte = "RWXF".getBytes();
			    	System.arraycopy(bufByte, 0, buf, 0, bufByte.length);
			    	byte[] idByteS = writeBigEndianInt(2);
			    	System.arraycopy(idByteS, 0, buf, 4, idByteS.length);
			    	byte[] statusByteS = writeBigEndianInt(2);
			    	System.arraycopy(statusByteS, 0, buf, 8, statusByteS.length);
			    	out.write(buf);
			    	out.flush();
			    	
			    	
			    	strByte = new byte[4];
			    	in.read(strByte);
			    	statusByte = new byte[4];
			    	in.read(statusByte);
			    	byte[] idsByte = new byte[8];
			    	in.read(idsByte);
			    	byte[] lenByte = new byte[8];
			    	in.read(lenByte);
			    	str = new String(strByte);
			    	status = readBigEndianInt(statusByte);
			    	long ids = readBigEndianLong(idsByte);
			    	long len = readBigEndianLong(lenByte);
			    	
			    	System.out.println("接受到的任务下发--包头："+str);
			    	System.out.println("接受到的任务下发--状态："+status);
			    	System.out.println("接受到的任务下发--数据ID："+ids);
			    	System.out.println("接受到的任务下发--数据长度："+len);
			    	
			    	if(str.equals("RWXF")&&status==3&&ids==1L){
			    		System.out.println("通信符合要求开始接收数据");
			    		byte[] dataStr = new byte[Long.valueOf(len).intValue()];
				    	in.read(dataStr);
				    	String xml = new String(dataStr);
				        System.out.println("接收到任务下发的数据:"+xml);
				        
				        int Strlen = "<root><sex>1</sex><name>XXX</name><age>18</age></root>".getBytes("UTF-8").length;
			    		int length = 4+4+8+8+Strlen;
			    		byte[] buf2 = new byte[length];
			    		bufByte = "RWXF".getBytes();
				    	System.arraycopy(bufByte, 0, buf2, 0, bufByte.length);
				    	
				    	idByteS = writeBigEndianInt(4);
				    	System.arraycopy(idByteS, 0, buf2, 4, idByteS.length);
				    	
				    	byte[] dataIdByte = writeBigEndianLong(1);
				    	System.arraycopy(dataIdByte, 0, buf2, 8, dataIdByte.length);
				    	
				    	byte[] dataLenByte = writeBigEndianLong(Strlen);
				    	System.arraycopy(dataLenByte, 0, buf2, 16, dataLenByte.length);

				    	byte[] dataByte = "sdfjsfodsfmah".getBytes("UTF-8");
				    	System.arraycopy(dataByte, 0, buf2, 24, dataByte.length);
				    	out.write(buf2);
				    	out.flush();
			    	}else{
			    		System.out.println("通信协议出错，本次通话结束");
			    	}
		    	}else{
		    		System.out.println("任务接受失败，结束本次会话");
		    	}
		    	
		    	
		    	
		    	
		    	
		    	
				/*
				if(str.equals("END")){
					System.out.println("**************This IO Handler Is End***********");
					outNew.close();
					break;
				}else{
					if(myFile==null){
						new File("D:\\file\\").mkdirs();
						myFile = new File("D:\\file\\"+System.currentTimeMillis()+".cng");
						outNew = new RandomAccessFile(myFile,"rw");
					}
					outNew.writeBytes(str);
				}
				*/
		    	in.close();
		    	out.close();
				System.out.println("closing...");
			}
		} catch (IOException e) {
		} finally {
			try {
				socket.close();
			} catch (IOException e) {
			}
		}
	}
	public final static byte[] writeInt(int n){
	 	byte[] b = new byte[4];   
        b[0] = (byte) (n >> 24 & 0xff);   
        b[1] = (byte) (n >> 16 & 0xff);   
        b[2] = (byte) (n >> 8 & 0xff);   
        b[3] = (byte) (n >> 0 & 0xff);
        return b;
    }
 private static byte[] writeBigEndianInt(int n) {   
        byte[] b = new byte[4];   
        b[0] = (byte) (n & 0xff);   
        b[1] = (byte) (n >> 8 & 0xff);   
        b[2] = (byte) (n >> 16 & 0xff);   
        b[3] = (byte) (n >> 24 & 0xff);   
       return b;   
    } 
 public final static byte[] writeLong(long v) throws IOException {
	   byte[] writeBuffer = new byte[8];
        writeBuffer[0] = (byte)(v >>> 56);
        writeBuffer[1] = (byte)(v >>> 48);
        writeBuffer[2] = (byte)(v >>> 40);
        writeBuffer[3] = (byte)(v >>> 32);
        writeBuffer[4] = (byte)(v >>> 24);
        writeBuffer[5] = (byte)(v >>> 16);
        writeBuffer[6] = (byte)(v >>>  8);
        writeBuffer[7] = (byte)(v >>>  0);
        return writeBuffer;
    }
 private static byte[] writeBigEndianLong(long n){
        byte[] b = new byte[8];   
        b[0] = (byte) (n & 0xff);   
        b[1] = (byte) (n >> 8 & 0xff);   
        b[2] = (byte) (n >> 16 & 0xff);   
        b[3] = (byte) (n >> 24 & 0xff);
        b[4] = (byte) (n >> 32 & 0xff);   
        b[5] = (byte) (n >> 40 & 0xff);   
        b[6] = (byte) (n >> 48 & 0xff);
        b[7] = (byte) (n >> 56 & 0xff);
        return b;
 }
 public final static int readInt(byte[] b) throws IOException {
     int ch1 = b[0];
     int ch2 = b[1];
     int ch3 = b[2];
     int ch4 = b[3];
     if ((ch1 | ch2 | ch3 | ch4) < 0)
         throw new EOFException();
     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
 }
public final static int readBigEndianInt(byte[] b) throws IOException {
     int ch1 = b[0];
     int ch2 = b[1];
     int ch3 = b[2];
     int ch4 = b[3];
     if ((ch1 | ch2 | ch3 | ch4) < 0)
         throw new EOFException();
     return ((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24));
 }

public final static long readLong(byte[] b) throws IOException {
     return (((long)b[0] << 56) +
             ((long)(b[1] & 255) << 48) +
             ((long)(b[2] & 255) << 40) +
             ((long)(b[3] & 255) << 32) +
             ((long)(b[4] & 255) << 24) +
             ((b[5] & 255) << 16) +
             ((b[6] & 255) <<  8) +
             ((b[7] & 255) <<  0));
 }
public final static long readBigEndianLong(byte[] b) throws IOException {
     return (((long)b[0] << 0) +
             ((long)(b[1] & 255) << 8) +
             ((long)(b[2] & 255) << 16) +
             ((long)(b[3] & 255) << 24) +
             ((long)(b[4] & 255) << 32) +
             ((b[5] & 255) << 40) +
             ((b[6] & 255) << 48) +
             ((b[7] & 255) << 56));
 }
}
